﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32.TaskScheduler;
using System.Security.Principal;
using System.Windows.Forms;
using System.IO;

namespace FolderCalendar
{ 
    class TaskManager
    {
        /// <summary>
        /// Checks if task is already in Task Scheduler
        /// </summary>
        public static void CheckTask()
        {
            TaskService ts = new TaskService();

            if (ts.GetTask(@"\Live@edu-Exchange Calendar Synchronizer") == null)
                RegisterTask();
            
        }

        public static void RegisterTask()
        {
            TaskService ts = new TaskService();
            TaskDefinition td = ts.NewTask();
            td.RegistrationInfo.Author = WindowsIdentity.GetCurrent().Name;
            td.RegistrationInfo.Date = DateTime.Now;
            td.RegistrationInfo.Description = "Calls utility which synchronizes users calendars beetween 2 Exchange Servers.";
            td.RegistrationInfo.Version = new Version(1, 0, 0, 0);
            td.RegistrationInfo.Documentation = @"For additional documentation see: http://liveexchangesync.codeplex.com/";
            
            td.Triggers.Add(new DailyTrigger { DaysInterval = 1 });
            
            td.Actions.Add(new ExecAction(Directory.GetCurrentDirectory()+"FolderCalendar.exe", null, null));

            ts.RootFolder.RegisterTaskDefinition("Live@edu - Exchange Calendar Synchronizer", td);
        }
    }
}
